package com.brayan.webapp.controller.general.converter;

import javax.faces.bean.ManagedBean;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.inject.Inject;

import com.brayan.webapp.model.PubThread;
import com.brayan.webapp.repository.PubThreadRepository;

@ManagedBean(name = "pubThreadConverterController")
@FacesConverter(value = "pubThreadConverter")
public class PubThreadConverter implements Converter {

	@Inject
	private PubThreadRepository pubThreadRepository;

	public PubThreadConverter() {
	}

	// from page to backing bean (create whatever is needed in the setter
	// method)
	/** In this case, return a list */
	@Override
	public Object getAsObject(FacesContext ctx, UIComponent component,
			String value) {
		Long pubThreadId = Long.valueOf(value);

		PubThread newPubThread = pubThreadRepository
				.getPubThreadById(pubThreadId);

		return newPubThread;
	}

	// from backing bean to page (send the id ob the object)
	@Override
	public String getAsString(FacesContext fc, UIComponent uic, Object o) {
		PubThread pubThread = ((PubThread) o);
		return pubThread.getId().toString();
	}

}
